import { INativeHelper } from "../../app_base/module/native/INativeHelper";

/**
 @name: NativeHelper
@desc: cocos 2.4.x 下nativeHelper
@author: timoo
@date: 2022/12/02
*/
export class CocosNativeHelper extends INativeHelper {

    public callWithReturnType(className: string, methodName: string, returnType: string | number | boolean, ...args: any[]): string | number | boolean {
        if (!className) {
            return;
        }
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            let argsSig = this.getArgsSig(args, returnType);
            console.log("callWithReturnType:", className, methodName, argsSig, args.join(","));
            return jsb.reflection.callStaticMethod(className, methodName, argsSig, ...args);
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            //todo
        }
    }

    private getArgsSig(args: any[], returnType: any): string {
        let sig: string[] = ["("];
        for (let i = 0; i < args.length; i++) {
            const element = args[i];
            sig.push(this.getValueTypeStr(element))
        }
        sig.push(")", this.getValueTypeStr(returnType))
        return sig.join("");
    }

    private getValueTypeStr(value: any): string {
        if (value == null || value == undefined) {
            return "V";
        }
        let typeStr = typeof value;
        if (typeStr == "number") {
            if (value % 1 != 0) { //float
                return "F"
            } else {
                return "I"
            }
        } else if (typeStr == "boolean") {
            return "Z"
        } else if (typeStr == "string") {
            return "Ljava/lang/String;"
        }
    }
}
